﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;

namespace T.Personnel.Repository
{
    public class BaseRepository<T> : IRepository<T> where T : class
    {
        private readonly EFDbcontext eFDbcontext;

        public BaseRepository(EFDbcontext eFDbcontext)
        {
            this.eFDbcontext = eFDbcontext;            
        }

        public async Task<int> AddAsync(T t)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            await values.AddAsync(t);
            var count = await eFDbcontext.SaveChangesAsync();
            if (count != 0)
            {
                return (int)t.GetType().GetProperty("Id")?.GetValue(t);
            }
            else
            {
                return 0;
            }
        }

        public async Task<int> AddsAsync(T t)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            await values.AddAsync(t);
            var count = await eFDbcontext.SaveChangesAsync();
            if (count != 0)
            {
                return (int)t.GetType().GetProperty("ApprovalRecordId")?.GetValue(t);
            }
            else
            {
                return 0;
            }
        }

        public async Task<int> DelAsync(int id)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            var allor = await values.FindAsync(id);
            if (allor != null)
            {
                values.Remove(allor);                
                return await eFDbcontext.SaveChangesAsync();
            }
            return -1;
        }

        public async Task<int> DelAsync(Expression<Func<T, bool>> predicate)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            var alltor = await values.FindAsync(predicate);
            if(alltor != null)
            {
                values.Remove(alltor);
                return await eFDbcontext.SaveChangesAsync();
            }
            return -1;
        }

        public async Task<T> GetAsync(int id)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            var allor = await values.FindAsync(id);
            return allor;
        }

        public async Task<T> GetAsync(Expression<Func<T, bool>> predicate)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            var allor = await values.FirstOrDefaultAsync(predicate);
            return allor;
        }

        public async Task<IQueryable<T>> GetListAsync(Expression<Func<T, bool>> predicate)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            var gather = values.AsNoTracking().Where(predicate);
            return gather;
        }

        public async Task<int> UpdAsync(T t)
        {
            DbSet<T> values = eFDbcontext.Set<T>();
            values.Update(t);
            return await eFDbcontext.SaveChangesAsync();            
        }
    }
}
